﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChatClient
{
    interface ISettingsPanelDelegate
    {
        void OnConnectAction(String serverAddress, String username);
        void OnDisconnectAction();
        void OnJoinRoomAction(String roomName);
    }

    class SettingsPanel : GroupBox
    {
        // Private constants

        private const int kSettingsPanelHeight = 103; //78
        private const int kLeftItemsOffset = 6;
        private const int kTopItemsOffset = 19;

        private const int kMarginBetweenItems = 5;
        private const int kItemsHeight = 20;
        private const int kLabelsWidth = 80;
        private const int kTextBoxesWidth = 142;
        private const int kConnectButtonWidth = 75;
        private const int kJoinRoomButtonWidth = 75;

        private const String kSettingsTitle = "Settings";
        private const String kServerAddressString = "Server Address";
        private const String kUsernameString = "Username";
        private const String kConnectButtonText = "Connect";
        private const String kDisconnectButtonText = "Disconnect";
        private const String kJoinRoomString = "Room";
        private const String kJoinRoomButtonText = "Join";

        // Private members

        private ISettingsPanelDelegate m_delegate = null;

        private bool m_connected = false;

        private Label m_serverAddressLabel = null;
        private TextBox m_serverAddress = null;
        private Label m_usernameLabel = null;
        private TextBox m_username = null;
        private Button m_connectButton = null;
        private Label m_roomLabel = null;
        private TextBox m_room = null;
        private Button m_roomButton = null;

        // Constructor

        public SettingsPanel(ISettingsPanelDelegate panelDelegate, int left, int top, int width, Control parent)
        {
            System.Diagnostics.Debug.Assert(panelDelegate != null);

            this.Parent = parent;
            this.SetBounds(left, top, width, kSettingsPanelHeight);
            this.Text = kSettingsTitle;
            this.m_delegate = panelDelegate;

            m_serverAddressLabel = new Label();
            m_serverAddressLabel.Parent = this;
            m_serverAddressLabel.Text = kServerAddressString;
            m_serverAddressLabel.SetBounds(kLeftItemsOffset, kTopItemsOffset, kLabelsWidth, kItemsHeight);

            m_serverAddress = new TextBox();
            m_serverAddress.Parent = this;
            m_serverAddress.Text = "192.168.253.2";
            m_serverAddress.SetBounds(m_serverAddressLabel.Right + kMarginBetweenItems, kTopItemsOffset, kTextBoxesWidth, kItemsHeight);

            m_usernameLabel = new Label();
            m_usernameLabel.Parent = this;
            m_usernameLabel.Text = kUsernameString;
            m_usernameLabel.SetBounds(kLeftItemsOffset, m_serverAddressLabel.Bottom + kMarginBetweenItems, kLabelsWidth, kItemsHeight);

            m_username = new TextBox();
            m_username.Parent = this;
            m_username.Text = String.Empty;
            m_username.SetBounds(m_usernameLabel.Right + kMarginBetweenItems, m_usernameLabel.Top, kTextBoxesWidth, kItemsHeight);

            m_connectButton = new Button();
            m_connectButton.Parent = this;
            m_connectButton.Text = kConnectButtonText;
            m_connectButton.SetBounds(m_username.Right + kMarginBetweenItems, m_username.Top, kConnectButtonWidth, kItemsHeight);

            m_roomLabel = new Label();
            m_roomLabel.Parent = this;
            m_roomLabel.Text = kJoinRoomString;
            m_roomLabel.SetBounds(kLeftItemsOffset, m_usernameLabel.Bottom + kMarginBetweenItems, kLabelsWidth, kItemsHeight);

            m_room = new TextBox();
            m_room.Parent = this;
            m_room.Text = String.Empty;
            m_room.SetBounds(m_roomLabel.Right + kMarginBetweenItems, m_roomLabel.Top, kTextBoxesWidth, kItemsHeight);

            m_roomButton = new Button();
            m_roomButton.Parent = this;
            m_roomButton.Text = kJoinRoomButtonText;
            m_roomButton.SetBounds(m_room.Right + kMarginBetweenItems, m_room.Top, kJoinRoomButtonWidth, kItemsHeight);
            m_roomButton.Enabled = m_connected; //I'm not sure if it's necessary

            m_connectButton.Click += new EventHandler((object sender, EventArgs e) =>
            {
                if (m_connected)
                {
                    m_delegate.OnDisconnectAction();
                }
                else
                {
                    m_delegate.OnConnectAction(m_serverAddress.Text, m_username.Text);
                }
                m_roomButton.Enabled = m_connected;
            });

            m_username.KeyPress += new KeyPressEventHandler((object sender, KeyPressEventArgs e) =>
            {
                if (e.KeyChar != (char) Keys.Enter)
                {
                    return;
                }

                if (m_connected)
                {
                    m_delegate.OnDisconnectAction();
                }
                else
                {
                    m_delegate.OnConnectAction(m_serverAddress.Text, m_username.Text);
                }
                m_roomButton.Enabled = m_connected;
            });

            m_roomButton.Click += new EventHandler((object sender, EventArgs e) =>
            {
                m_delegate.OnJoinRoomAction(m_room.Text);
                m_room.Text = String.Empty;
            });

            m_room.KeyPress += new KeyPressEventHandler((object sender, KeyPressEventArgs e) =>
            {
                if (e.KeyChar != (char)Keys.Enter)
                {
                    return;
                }
                m_delegate.OnJoinRoomAction(m_room.Text);
                m_room.Text = String.Empty;
            });

            this.Controls.Add(m_serverAddressLabel);
            this.Controls.Add(m_serverAddress);
            this.Controls.Add(m_usernameLabel);
            this.Controls.Add(m_username);
            this.Controls.Add(m_connectButton);
            this.Controls.Add(m_roomLabel);
            this.Controls.Add(m_room);
            this.Controls.Add(m_roomButton);

            this.GotFocus += new EventHandler(OnGotFocus);
        }

        // Public methods

        public void SetConnectedState()
        {
            m_connected = true;
            m_connectButton.Text = kDisconnectButtonText;
            m_serverAddress.Enabled = false;
            m_username.Enabled = false;
        }

        public void SetDisconnectedState()
        {
            m_connected = false;
            m_connectButton.Text = kConnectButtonText;
            m_serverAddress.Enabled = true;
            m_username.Enabled = true;
        }

        public String CurrentUsername()
        {
            return m_username.Text;
        }

        // Events handlers

        public void OnGotFocus(object sender, EventArgs e)
        {
            m_username.Focus();
        }
    }
}
